#include "SkinContainerWnd.h"

CSkinContainerWnd::CSkinContainerWnd()
{
}

CSkinContainerWnd::~CSkinContainerWnd()
{
}

BOOL CSkinContainerWnd::IsContainer()
{
    return TRUE;
}

void CSkinContainerWnd::LayoutChild( CSkinWindowBase* pWndChild )
{

}

/////////////////////////////CSkinDiv/////////////////////////////////////////////
CSkinDiv::CSkinDiv()
{
}

CSkinDiv::~CSkinDiv()
{
}

void CSkinDiv::OnWindowPosChanged( LPWINDOWPOS lpWndPos )
{
    CSkinWindowBase::OnWindowPosChanged( lpWndPos );
    OnChildWindowPosChanged( lpWndPos );

//	LayoutChilds();
}

LRESULT CSkinDiv::OnNcCalcSize( BOOL bCalcValidRects, LPARAM lParam )
{
    CSkinWindowBase::OnNcCalcSize( bCalcValidRects, lParam );

    std::vector<CSkinWindowBase*>::const_iterator cit;

    for ( cit = m_vecChildObj.begin(); cit != m_vecChildObj.end(); cit++ )
    {
        CSkinWindowBase* pWndBase = *cit;

        if ( NULL != pWndBase )
        {
            pWndBase->OnNcCalcSize( bCalcValidRects, lParam );
        }
    }

    return 0;
}

void CSkinDiv::LayoutChild( CSkinWindowBase* pWndChild )
{
    if ( NULL != pWndChild )
    {
        WINDOWPOS WndPos;
        WndPos.hwnd = NULL;
        WndPos.hwndInsertAfter = NULL;
        WndPos.flags = SWP_SHOWWINDOW;
        WndPos.x = m_rcWindow.left;
        WndPos.y = m_rcWindow.top;
        WndPos.cx = m_rcWindow.Width();
        WndPos.cy = m_rcWindow.Height();

        pWndChild->OnWindowPosChanged( &WndPos );
    }
}

void CSkinDiv::LayoutChilds()
{
    std::vector<CSkinWindowBase*>::const_iterator cit;

    for ( cit = m_vecChildObj.begin(); cit != m_vecChildObj.end(); cit++ )
    {
        CSkinWindowBase* pWndBase = *cit;

        if ( NULL != pWndBase )
        {
            LayoutChild( pWndBase );
        }
    }
}

void CSkinDiv::OnPaint( HDC hDC )
{
    __super::OnPaint( hDC );
    std::vector<CSkinWindowBase*>::const_iterator cit;

    for ( cit = m_vecChildObj.begin(); cit != m_vecChildObj.end(); cit++ )
    {
        CSkinWindowBase* pWndBase = *cit;

        if ( NULL != pWndBase )
        {
            pWndBase->OnPaint( hDC );
        }
    }
}

BOOL CSkinDiv::LoadNode( IXMLDOMNode* pNode )
{
    if ( !__super::LoadNode( pNode ) )
        return FALSE;

    return LoadChild( pNode );
}

BOOL CSkinDiv::LoadChild( IXMLDOMNode* pNode )
{
    CComPtr<IXMLDOMNodeList> childNodeList;
    pNode->get_childNodes( &childNodeList );
    long nodeCount = 0;
    childNodeList->get_length( &nodeCount );

    for ( long i = 0; i < nodeCount; i++ )
    {
        CComBSTR nodeName;
        CComPtr<IXMLDOMNode> childNode;
        childNodeList->get_item( i, &childNode );
        childNode->get_nodeName( &nodeName );
        CSkinWindowBase* pWndBase = CreateObject( nodeName );

        if ( NULL == pWndBase )
        {
            continue;
        }

        pWndBase->SetRealParentHwnd( GetRealHwnd() );
        pWndBase->SetContainer( this );
        pWndBase->LoadNode( childNode );
        pWndBase->NodeLoadComplete();
        m_vecChildObj.push_back( pWndBase );
    }

    return TRUE;
}

void CSkinDiv::OnChildWindowPosChanged( LPWINDOWPOS lpWndPos )
{
    WINDOWPOS WndPos = {0, 0, m_rcWindow.left, m_rcWindow.top, lpWndPos->cx, lpWndPos->cy, SWP_SHOWWINDOW};
    std::vector<CSkinWindowBase*>::const_iterator cit;

    for ( cit = m_vecChildObj.begin(); cit != m_vecChildObj.end(); cit++ )
    {
        CSkinWindowBase* pWndBase = *cit;

        if ( NULL != pWndBase )
        {
            pWndBase->OnWindowPosChanged( &WndPos );
            WndPos.x += m_rcWindow.left;
            WndPos.y += m_rcWindow.Height();
// 			WndPos.cx+=WndPos.x;
// 			WndPos.cy+=WndPos.y;
        }
    }
}
